<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    protected $db;

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @param $string 参数拼接的字符串
     * @param $method 请求方式：put、post、get、delete
     * @return $json
     */
    public function setCurl($url = '', $string = '', $method = 'get')
    {
        if (!$url) {
            return array('code'=>-2 ,'msg'=>'没有接口地址');
        }
        if (!$method) {
            return array('code'=>-2 ,'msg'=>'没有请求方式');
        }
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($method=='post') {
            curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
            curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
            curl_setopt($curl, CURLOPT_ENCODING, '');
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json', 'Content-Length: ' . strlen($string)
            ));
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code' => -1, 'msg' => '请求失败');
        }
        $items = json_decode($output,true);
        return array('code' => 0,'msg' => 'success', 'data' => $items);
    }

    /**
     * 计算指定目录大小
     */
    public function getDirSize($dir)
    {
        @$dh = opendir($dir);
        $size = 0;
        while ($file = @readdir($dh)) {
            if ($file != "." and $file != "..") {
                $path = $dir."/".$file;
                if (is_dir($path)) {
                    $size += $this->getDirSize($path);
                } elseif (is_file($path)) {
                    $size += filesize($path);
                }
            }
        }
        @closedir($dh);
        return $size;
    }

    /**
     * 计算指定目录文件个数
     */
    public function getDirNum($dir)
    {
        @$dh = opendir($dir);
        $num = 0;
        while ($file = @readdir($dh)) {
            if ($file != "." and $file != "..") {
                $path = $dir."/".$file;
                if (is_dir($path)) {
                    $num += $this->getDirNum($path);
                } elseif (is_file($path)) {
                    $num ++;
                }
            }
        }
        @closedir($dh);
        return $num;
    }

    /**
     * 文件大小分级
     */
    public function getSizeLevel($size)
    {
        if ($size>1024 && $size<=1024*1024) {
            return round($size/1024,2) . 'KB';
        }
        else if ($size>1024*1024 && $size<=1024*1024*1024) {
            return round($size/1024/1024,4) . 'MB';
        }
        else if ($size>1024*1024*1024 && $size<=1024*1024*1024*1024) {
            return round($size/1024/1024/1024,6) . 'GB';
        }
        else if ($size>1024*1024*1024*1024) {
            return round($size/1024/1024/1024/1024,8) . 'TB';
        }
        else {
            return $size . 'B';
        }
    }
}
